/*!
 *
 * \file    Mxd0250OAL.h
 *
 * \brief   SDK OAL header file. this file must porting to fix target os.
 *
 * Header file of the OAL declaration about MIRA CMMB Demod SDK
 *
 * \par    Include files
 * - Mxd0250OAL.h
 *
 * \par    Copyright (c) 2008 Maxscend Technologies Inc. All rights reserved
 *
 * PROPRIETARY RIGHTS of Maxscend Technologies Inc. are involved in
 * the subject matter of this material.  All manufacturing, reproduction,
 * use, and sales rights pertaining to this subject matter are governed
 * by the license agreement.  The recipient of this software implicitly
 * accepts the terms of the license.
 *
 * \version
 * Revision of last commit: $Rev:: 4                                        $
 * Author of last commit  : $Author:: maxscend\chaohui.jiang                $
 * Date of last commit    : $Date:: 2008-11-17 15:45:38 +0800 (星期?$
 *
 */

#ifndef _MXDTYPES_H
#define _MXDTYPES_H

#ifdef __cplusplus
#define __INLINE__              inline
#else
#define __INLINE__              __inline
#endif /*__cplusplus*/

#ifdef __cplusplus
extern "C"
{
#endif

#ifdef DLL_EXPORTS
#define MXD_EXPORT __declspec(dllexport)
#else
#define MXD_EXPORT
#endif

#ifndef FALSE
#define FALSE             0
#define TRUE              1
#endif

#ifndef NULL
#define NULL              0
#endif

    typedef void *MXD_HANDLE;

#ifdef WIN32
#define MXD_API __stdcall
#else
#define MXD_API
#endif

    typedef signed int     MXD_BOOL;

    typedef char              MXD_INT8;
    typedef char              MXD_CHAR;
    typedef unsigned char     MXD_UINT8;

    typedef short             MXD_SHORT;
    typedef unsigned short    MXD_USHORT;

    typedef short             MXD_INT16;
    typedef unsigned short    MXD_UINT16;

    typedef int               MXD_INT;
    typedef unsigned int      MXD_UINT;

    typedef int               MXD_INT32;
    typedef unsigned int      MXD_UINT32;

    typedef float             MXD_FLOAT;

    typedef double            MXD_DOUBLE;

    typedef unsigned long     MXD_ULONG;

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>

#ifdef MIRA_OAL_FUNC_IMP

#include "master.h"


    void *MXD_Memcpy(void *dst, const void *src, MXD_INT32 len)
    {
        return  memcpy(dst, src, len);
    }

    void *MXD_Memset(void *dst, MXD_INT32 c, MXD_INT32 len)
    {
        return memset(dst, c, len);
    }

    MXD_INT32 MXD_Memcmp(const void *mem1, const void *mem2, MXD_INT32 len)
    {
        return memcmp(mem1, mem2, len);
    }

    void *MXD_Strcpy(MXD_CHAR *dst, const MXD_CHAR *src)
    {
        return strcpy(dst, src);
    }

    MXD_INT32 MXD_Strcmp(const MXD_CHAR *str1, const MXD_CHAR *str2)
    {
        return strcmp(str1, str2);
    }

    MXD_UINT32 MXD_Strlen(const MXD_CHAR *str)
    {
        return (MXD_UINT32)strlen(str);
    }

    void MXD_DebugPrint(MXD_CHAR* outFmt, ...);
    extern unsigned char g_pMxdFrameBuffer[ 100*1000 ];
    extern MXD_UINT8 g_pMxdDemuxBuf[ 44*4000 ];

    void* MXD_Malloc(MXD_UINT32 size)
    {

        if (size < 100)
        {
            return (void*)MC_RTK_GET_MEMORY(size);
        }

        if (size == 55296)
        {
            //return g_pMxdFrameBuffer;
        }

        if (size == 4000*44)
        {
            return g_pMxdDemuxBuf;
        }

        if (!size)
            MXD_DebugPrint("malloc 0!!!!!!!\n");

        return (void*)MC_RTK_GET_DYN_POOL_MEMORY(AUDIO_VIDEO_POOL, size);

        //return ;//malloc( msize );
    }

    void MXD_Free(void *pMemToFree)
    {
        //if (pMemToFree == g_pMxdFrameBuffer )
        //return;
        if (pMemToFree == g_pMxdDemuxBuf)
            return;

        if (pMemToFree)
            MC_RTK_FREE_MEMORY(pMemToFree);

        //free( pmem );
    }
    __INLINE__  MXD_CHAR *MXD_Strdup(MXD_CHAR *s)
    {
        MXD_CHAR *pNewStr;

        if (s == NULL)
        {
            //printf("strdup------------ null!\n");
            return NULL;
        }

        pNewStr = (MXD_CHAR *)MXD_Malloc(MXD_Strlen(s) + 1);
        MXD_Strcpy(pNewStr, s);
        return pNewStr;
    }
#define MSG_OUT_SIZE 256
    char msBuf[MSG_OUT_SIZE];
    extern int cmmbdrv_log;
    extern bool din2_50SendByteOnUart(u8 vp_byte, u8 vp_uart);

    void MXD_DebugPrint(MXD_CHAR* outFmt, ...)
    {
#define TRACE_BUF_SIZE 256
        int nBuf;
        u8 szBuffer[TRACE_BUF_SIZE];
        char *ptr;
        va_list args;
        //cmmbdrv_log = 1;

        if (0 == cmmbdrv_log)
            return;

        va_start(args, outFmt);
        nBuf = _vsnprintf((char *)szBuffer, TRACE_BUF_SIZE - 1, outFmt, args);
        ptr = (char *)szBuffer;


        if (1 == cmmbdrv_log)
        {
            while (*ptr)
                din2_50SendByteOnUart(*ptr++, 0);

            if ('$' != szBuffer[0])
            {
                din2_50SendByteOnUart('\r', 0);
                din2_50SendByteOnUart('\n', 0);
            }
        }
        else if (2 == cmmbdrv_log)
        {
            // t_gfl_FILE  *p_Stream = NULL;
            int len = 0;
            strcat(ptr, "\r\n");

            while (0 != *(ptr + len))
            {
                len++;
            }

            cmmb_write_log_to_file("/share1/other/mbbmslog.txt", ptr, len);

        }
        else if (3 == cmmbdrv_log)
        {
            strcat(ptr, "\r\n");
            nBuf += 2;
            tra1_2TraceApplOutput(PROCESS_ENV, 0, 0xFF, (u8*)szBuffer, (s16)TRACE_BUF_SIZE, &nBuf);
        }

        va_end(args);
    }

#else
    void *MXD_Memcpy(void *dst, const void *src, MXD_INT32 len);

    void *MXD_Memset(void *dst, MXD_INT32 c, MXD_INT32 len);

    MXD_INT32 MXD_Memcmp(const void *mem1, const void *mem2, MXD_INT32 len);

    void *MXD_Strcpy(MXD_CHAR *dst, const MXD_CHAR *src);

    MXD_INT32 MXD_Strcmp(const MXD_CHAR *str1, const MXD_CHAR *str2);

    MXD_UINT32 MXD_Strlen(const MXD_CHAR *str);

    void* MXD_Malloc(MXD_UINT32 msize);

    void MXD_Free(void *pmem);

    void MXD_DebugPrint(MXD_CHAR* dbgOutput, ...);

    void* MXD_Sleep(MXD_UINT32 milliSeconds);

#endif

#ifdef __cplusplus
}
#endif

#endif /* _MXDTYPES_H */
